package kt.hkit.androidcomplete01.chapter08;

import kr.hkit.androidcomplete01.R;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;

public class MoveCircleEx extends Activity {
	MyView mv;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mv = new MyView(this);
		mv.setOnLongClickListener(mLongClickListener);
		setContentView(mv);
	}
	
	View.OnLongClickListener mLongClickListener = new View.OnLongClickListener() {
		public boolean onLongClick(View v) {
			if(mv.mColor == Color.BLUE){
				mv.mColor = Color.RED;
			}
			return true;
		}
	};
	
	class MyView extends View {
		float mX;
		float mY;
		int mColor;
		Paint paint;

		public MyView(Context context) {
			super(context);
			mX = 100;
			mY = 100;
			mColor = Color.BLUE;
			paint = new Paint(Paint.ANTI_ALIAS_FLAG);
			paint.setColor(mColor);
		}

		public boolean onTouchEvent(MotionEvent event) {
			if (event.getAction() == MotionEvent.ACTION_DOWN) {
				mX = event.getX();
				mY = event.getY();
				invalidate();
				return true;
			}
			return false;
		}
		
		protected void onDraw(Canvas canvas) {
			canvas.drawColor(Color.LTGRAY);
			paint.setColor(mColor);
			canvas.drawCircle(mX, mY, 16, paint);
		}

	}
}
